package com.sc.service.serviceImpl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.sc.common.Bo.ItemParamBo;
import com.sc.common.pojo.CommonResult;
import com.sc.common.pojo.EasyUiDataGridResult;
import com.sc.mapper.TbItemParamMapper;
import com.sc.pojo.TbItemCatExample;
import com.sc.pojo.TbItemParam;
import com.sc.pojo.TbItemParamExample;
import com.sc.service.ItemParamService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * Created by sc on 2017/10/16.
 */
@Service
public class ItemParamServiceImpl implements ItemParamService {

    @Autowired
    private TbItemParamMapper itemParamMapper;


    @Override
    public EasyUiDataGridResult getParamList(int page, int rows) {
        PageHelper.startPage(page,rows);

        List<ItemParamBo> list = itemParamMapper.selectAllParam();
        PageInfo<ItemParamBo> pageInfo = new PageInfo<ItemParamBo>(list);
        EasyUiDataGridResult result = new EasyUiDataGridResult();
        result.setTotal(pageInfo.getTotal());
        result.setRows(list);
        return result;
    }

    @Override
    public CommonResult queryDiffById(Long catId) {
        TbItemParamExample example = new TbItemParamExample();
        TbItemParamExample.Criteria criteria = example.createCriteria();
        criteria.andItemCatIdEqualTo(catId);
        List<TbItemParam> list = itemParamMapper.selectByExample(example);
        if(list!=null&&list.size()>0){
            TbItemParam tbItemParam = list.get(0);
            return CommonResult.ok(tbItemParam);
        }
        return CommonResult.ok();
    }

    @Override
    public CommonResult save(Long cid, String paramData) {
        TbItemParam tbItemParam = new TbItemParam();
        tbItemParam.setItemCatId(cid);
        tbItemParam.setParamData(paramData);
       itemParamMapper.insert(tbItemParam);
            return CommonResult.ok();

    }
}
